<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


// 检查手机号是否合法
if (!function_exists('checkPhone')){
    function checkPhone($phonenumber){
        if(preg_match("/^1\d{10}$/",$phonenumber)){
            return true;
        }else{
            return false;
        }
    }
}
// 检查身份证是否合法
if (!function_exists('validation_filter_id_card')) {
    function validation_filter_id_card($id_card)
    {
        if (strlen($id_card) == 18) {
            return idcard_checksum18($id_card);
        } elseif ((strlen($id_card) == 15)) {
            $id_card = idcard_15to18($id_card);
            return idcard_checksum18($id_card);
        } else {
            return false;
        }
    }
}
// 计算身份证校验码，根据国家标准GB 11643-1999
function idcard_verify_number($idcard_base)
{
    if (strlen($idcard_base) != 17) {
        return false;
    }
    //加权因子
    $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    //校验码对应值
    $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    $checksum = 0;
    for ($i = 0; $i < strlen($idcard_base); $i++) {
        $checksum += substr($idcard_base, $i, 1) * $factor[$i];
    }
    $mod = $checksum % 11;
    $verify_number = $verify_number_list[$mod];
    return $verify_number;
}

//15位身份证升级到18位
function idcard_15to18($idcard)
{
    if (strlen($idcard) != 15) {
        return false;
    } else {
        // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
        if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {
            $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);
        } else {
            $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);
        }
    }
    $idcard = $idcard . idcard_verify_number($idcard);
    return $idcard;
}

//8位身份证校验码有效性检查
function idcard_checksum18($idcard)
{
    if (strlen($idcard) != 18) {
        return false;
    }
    $idcard_base = substr($idcard, 0, 17);
    if (idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))) {
        return false;
    } else {
        return true;
    }
}


function get_param($str_Name,$mix_Type,$mix_Default = false,$map_Options = []){
    $request = request();
    $value = null;
    if($request->has($str_Name)){
        $value = $request->request($str_Name);

    }else if($mix_Default !== false){
        return $mix_Default;
    }else{
        throw new \Exception('必须的参数' . $str_Name . '不存在');
    }
    $arr_rules = ['phone','email','name','uint',"int", "str",'arr'];
    if(!in_array($mix_Type,$arr_rules)){
        throw new \Exception('检测规则' . $mix_Type . '不存在');
    }


    return check_param($value,$mix_Type,$map_Options);
}

function check_param($mix_Param,$mix_Type,$map_Options = []){

    if(is_array($mix_Type)){//枚举类型检测
        if(!in_array($mix_Param, $mix_Type)){
            throw new \Exception('参数不符合规则');
        }
        return $mix_Param;
    }else{//其他类型检测
        $cls_Check = [
            'phone' => function($value){
                return preg_match("/^1[345678]\d{9}$/", $value);
//        return preg_match("{^1[34578]\d{9}$}", $value);
            },
            'email' => function($value){
                return preg_match("{^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$}", $value);
            },
            'name' => function($value){
                return preg_match("/^[\x{4e00}-\x{9fa5}\w\d]+$/u", $value);
            },
            'uint' => function($value){
                return preg_match("{^\d+$}", $value);
            },
            "int" => function($value){
                return preg_match("{^-?\d+$}", $value);
            },
            "str" => function($value,$map_Options){
                return is_string($value) &&
                    (!isset($map_Options['max_len']) || strlen($value) <= $map_Options['max_len']) &&
                    (!isset($map_Options['min_len']) || strlen($value) >= $map_Options['min_len']) &&
                    (!isset($map_Options['not_empty']) || strlen($value) > 0);
            },
            'arr' => function($value,$map_Options){
                $boo_Check = isset($map_Options['not_empty']) || is_array($value);
                if(!$boo_Check){
                    return false;
                }
                $value = [];
                $boo_Check = (!isset($map_Options['max_len']) || $map_Options['max_len'] <= count($value)) &&
                    (!isset($map_Options['min_len']) || $map_Options['min_len'] >= count($value));
                if(!$boo_Check){
                    return false;
                }
                if(isset($map_Options['type'])){
                    try{
                        check_param($value, $map_Options['type']);
                    }catch(\Exception $ex){
                        return false;
                    }
                }
                return true;
            }
        ];
        if($cls_Check == null){
            throw new \Exception('检测规则' . $mix_Type . '不存在');
        }

        if(!$cls_Check[$mix_Type]($mix_Param,$map_Options)){
            throw new \Exception('参数不符合规则');
        }
    }
    return $mix_Param;
}

